﻿using System;
using System.Collections.Generic;
using System.Linq;
using ARMIR;
using ARMIR.util;
using RimWorld;
using Verse;

namespace ARMIR {
    /// <summary>
    /// 能力-战术装备03，在目标位置插入建筑使附近敌人减速
    /// </summary>
    public class CompAbilityEffect_TMZeroThree : CompAbilityEffect {
        public new CompProperties_TMZeroThree Props => (CompProperties_TMZeroThree)props;

        public override void Apply(LocalTargetInfo target, LocalTargetInfo dest) {
            base.Apply(target, dest);
            // 设置延迟
            TickUtil DelayTick = Current.Game.GetComponent<TickUtil>();
            speDraw.DragLight(parent.pawn.Map, parent.pawn.Position.ToVector3(), 10, 0.15f, 15, false, true, 0, 0.3f);
            DelayTick.TickTimeManager(20, () => {
                speDraw.DragLight(parent.pawn.Map, target.Cell.ToVector3(), 30, 0.15f, 30, false, false, 120, 0.8f);
            }, true);
            DelayTick.TickTimeManager(40, () => {
                //在落点生成特效
                EffecterDef TMLand = DefDatabase<EffecterDef>.GetNamed("ARMIR_TMZeroThree_EffecterA"); //内收环特效
                EffecterDef TMLandB = DefDatabase<EffecterDef>.GetNamed("ARMIR_TMZeroThree_EffecterAB"); //冲击波特效
                TMLand.Spawn(target.Cell, parent.pawn.Map);
                if (DelayTick != null)
                {
                    DelayTick.TickTimeManager(40, () => { TMLandB.Spawn(target.Cell, parent.pawn.Map); }, true);
                }
                //随机技能音效
                SoundDef randomSound = null;
                if (UnityEngine.Random.value > 0.5)
                {
                    randomSound = DefDatabase<SoundDef>.GetNamed("ARMIR_magicHitA");
                }
                else
                {
                    randomSound = DefDatabase<SoundDef>.GetNamed("ARMIR_magicHitB");
                }
                //03落地前造成一次范围伤害
                List<Thing> ExplosiveIgnoredPawns = new List<Thing>();
                ExplosiveIgnoredPawns.Add(parent.pawn);
                ExplosiveIgnoredPawns = parent.pawn.Map.mapPawns.AllPawnsSpawned.Where(p => p.Faction == Faction.OfPlayer && p.Position.DistanceTo(target.Cell) < Props.radius + 2).OfType<Thing>().ToList();
                int powerBase = (int)ARMIR_ModSettings.powerSet;
                GenExplosion.DoExplosion(
                    target.Cell,
                    parent.pawn.Map,
                    radius: Props.radius,
                    DamageDefOf.Blunt,
                    instigator: parent.pawn,
                    damAmount: (int)Props.damageAmount * powerBase,
                    armorPenetration: (int)Props.armorPenetration * powerBase,
                    damageFalloff: false, //伤害随距离衰减开启
                    screenShakeFactor: 0.4f, //屏幕震动强度(默认为1
                    explosionSound: randomSound,
                    ignoredThings: ExplosiveIgnoredPawns,
                    weapon: Props.weapon
                );
                //放置03柱
                Thing thing = ThingMaker.MakeThing(Props.spawnBuilding);
                thing.SetFactionDirect(parent.ConstantCaster.Faction);
                GenPlace.TryPlaceThing(thing, target.Cell, parent.pawn.Map, ThingPlaceMode.Direct, (Action<Thing, int>)null,
                    (Predicate<IntVec3>)null, default(Rot4));
            }, true);
            //负面效果
            bool hasEquipment = parent.pawn != null &&
            (parent.pawn.equipment.AllEquipmentListForReading.Any(e =>
            e.def == DefDatabase<ThingDef>.GetNamed("ARMIR_FPAMindMixer")) ||
            parent.pawn.apparel.WornApparel.Any(a => a.def == DefDatabase<ThingDef>.GetNamed("ARMIR_FPAMindMixer")));
            if (hasEquipment)
            {
                parent.pawn.needs.mood.thoughts.memories.TryGainMemory(DefDatabase<ThoughtDef>.GetNamed("ARMIR_MindLoad_weak")); //技能施放的思维负担
            }
            else
            {
                parent.pawn.needs.mood.thoughts.memories.TryGainMemory(DefDatabase<ThoughtDef>.GetNamed("ARMIR_MindLoad")); //技能施放的思维负担
            }
        }

        public override bool AICanTargetNow(LocalTargetInfo target) {
            return Valid(target);
        }

        public override bool Valid(LocalTargetInfo target, bool throwMessages = false) {
            if (target.Cell.Filled(parent.pawn.Map) ||
                (!Props.allowOnBuildings && target.Cell.GetEdifice(parent.pawn.Map) != null)) {
                if (throwMessages) {
                    Messages.Message(
                        "CannotUseAbility".Translate(parent.def.label) + ": " + "AbilityOccupiedCells".Translate(),
                        target.ToTargetInfo(parent.pawn.Map), MessageTypeDefOf.RejectInput, historical: false);
                }

                return false;
            }

            return true;
        }

        public override void DrawEffectPreview(LocalTargetInfo target) {
            float radius = 8;
            GenDraw.DrawRadiusRing(target.Cell, radius);
        }
    }
}